
class Circle: # 定义一个圆类
    def __init__(self, x, y, r, color,screen):
        self.x = x
        self.y = y
        self.r = r
        self.color = color
        self.screen = screen
        self.is_x_border = False
        self.is_y_border = False
        self.text = "1"
        self.speedx = 0
        self.speedy = 0

    def draw(self, pygame):
        # 绘制圆形
        pygame.draw.circle(self.screen, self.color, (self.x,self.y), self.r)
        if self.text != "":
            # 设置字体、大小
            font = pygame.font.Font('freesansbold.ttf', 15)
            # 绘制文字
            text_surface = font.render(self.text, True, (0, 0, 0))
            # 获取文字的矩形
            text_rect = text_surface.get_rect()
            # 设置文字的坐标
            text_rect.center = (self.x, self.y)
            # 绘制文字
            self.screen.blit(text_surface, text_rect)
    # 移动圆
    def move(self, dx, dy):
        self.speedy = dy
        self.speedx = dx
        self.moveX(dx)
        self.moveY(dy)
    def moveX(self, dx):
        if self.is_x_border == True:
            if self.x - self.r  > 0:
                self.x -= dx
            else:
                self.is_x_border = False
        else:
            if self.x  < self.screen.get_width() - self.r:
                self.x += dx
            else:
                self.is_x_border = True
    def moveY(self, dy):
        if self.is_y_border == True:
            if self.y - self.r > 0:
                self.y -= dy
            else:
                self.is_y_border = False
        else:
            if self.y + dy < self.screen.get_height() - self.r:
                self.y += dy
            else:
                self.is_y_border = True

    # 判断圆是否与另一个圆相交
    def isCollision(self, other):
        # 判断两个圆是否相交,计算圆心的距离，根据勾股定理进行计算
        dx = abs(self.x - other.x)
        dy = abs(self.y - other.y)
        result = False
        if dx**2 + dy**2 <= (self.r + other.r)**2:
            result = True
        # 打印两个圆的坐标
        print("2个圆的圆心坐标：", self.x, self.y, other.x, other.y)
        # 打印计算参数以及结果
        print("圆心距离：", dx, dy, "半径：", self.r, other.r, "相交结果：", result)
        print("-----------")
        return result

    # 与另外一个圆保持4倍r的距离
    def isCollision4r(self, other):
        # 判断两个圆是否相交,计算圆心的距离，根据勾股定理进行计算
        dx = abs(self.x - other.x)
        dy = abs(self.y - other.y)
        result = False
        if dx**2 + dy**2 <= (self.r + 2*other.r)**2:
            result = True
        # 打印两个圆的坐标
        print("2个圆的圆心坐标：", self.x, self.y, other.x, other.y)
        # 打印计算参数以及结果
        print("圆心距离：", dx, dy, "半径：", self.r, other.r, "相交结果：", result)
        print("-----------")
        return result

    # 追踪另外一个小球 type = True，直线追踪，type = False，圆润追踪
    def track(self, other,type):
        if other.x - self.x > 0:
            self.is_x_border = False
        else:
            self.is_x_border = True

        if other.y - self.y > 0:
            self.is_y_border = False
        else:
            self.is_y_border = True
        if type:
            return
        speed = 0.5/60
        # 定一个加速变量
        speed_add_x = False
        speed_add_y = False
        # 计算x的差值并且获取到绝对值
        dx = abs(other.x - self.x)
        # 距离目标5倍半径r的时候，开始加速追击
        if dx < other.r*5:
            speed_add_x = True
        # 计算y的差值并且获取到绝对值
        dy = abs(other.y - self.y)
        if dy < other.r*5:
            speed_add_y = True
        if speed_add_x and speed_add_y:
            speed = 4/60
            print("-----------------------靠近目标，开始加速，速度为：",dx*speed,dy*speed)

        self.move(dx*speed, dy*speed)

    # 判断圆是否与边界相交
    def isBorder(self):
        if self.x - self.r <= 0 or self.x + self.r >= self.screen.get_width():
            return True
        else:
            return False

    # 判断圆是否与边界相交
    def isBorderY(self):
        if self.y - self.r <= 0 or self.y + self.r >= self.screen.get_height():
            return True
        else:
            return False
    # 打印圆的信息
    def print(self):
        print("圆的坐标为：", self.x, self.y)
        print("圆的半径为：", self.r)
        print("圆的颜色为：", self.color)
        print("圆的屏幕为：", self.screen)
        print("圆的is_x_border为：", self.is_x_border)
        print("圆的is_y_border为：", self.is_y_border)
        print("圆的text为：", self.text)
        print("圆的速度为：", self.speedx, self.speedy)
        print("-----------------------------------")
